<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package		anime
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
	class Request extends My_Controller{
		
		public function __construct(){
			parent:: __construct();
			$this->load->model('mrequest');
			$this->load->library('form_validation');
			$this->load->helper('url');
			if(!$this->session->userdata("session_user") || $this->session->userdata("session_level") != 1){
				redirect(base_url()."admin/login");
			}
		}

		public function index(){
			$data['title']             = 'Danh sách liên hệ';
			$data['color']             = '#33925c';
			$data['template']          = 'request/list_request';
			$config['base_url']        = base_url('admin/request/index');
			$config['total_rows']      = $this->mrequest->count_all();
			$config['per_page']        = 20;
			$config['uri_segment']     = '4';
			$config['first_link']      = '<<';
			$config['last_link']       = '>>';
			$config['first_tag_open']  = '<li class="click">';
			$config['first_tag_close'] = '</li>';
			$config['last_tag_open']   = '<li class="click">';
			$config['last_tag_close']  = '</li>';
			$config['prev_tag_open']   = '<li class="click">';
			$config['prev_tag_close']  = '</li>';
			$config['next_tag_open']   = '<li class="click">';
			$config['next_tag_close']  = '</li>';
			$config['num_tag_open']    = '<li class="click">';
			$config['num_tag_close']   = '</li>';
			$config['next_link']       = 'Next';
			$config['prev_link']       = 'Prev';
			$config['cur_tag_open']    = '<li class="active"><a>';
			$config['cur_tag_close']   = '</a></li>';
			$this->load->library("pagination");
			$this->pagination->initialize($config);
			$start                     = $this->uri->segment(4)?$this->uri->segment(4):0;
			$param                     = array();
			$param['select']           = array('r_id','r_email','r_contents','r_status');
			$param['limit']            = array($config['per_page'] ,$start);	
			$data['result']            = $this->mrequest->get_list($param);

			$this->load->view("layout",$data);
		}

		public function edit(){

				
			$data['title']    = "Sửa thông tin request";
			$data['color']    = "#33925c";
			$data['template'] = 'request/edit_request';
			$var              = array();
			$id               = $this->uri->segment(4);
			if(isset($id)){
				if($this->input->post() != NULL){
					$data['title']    = 'Sửa thông tin request';
					$data['color']    = '#33925c';
					$data['template'] = 'request/edit_request';
					$param            = array('r_id','r_email','r_contents');
					$data['result']   = $this->mrequest->get_info($id,$param);
					$this->load->view('layout',$data);					
					$status      	  = 2;
					$db = array(
						'r_status' => $status,
					);
					$this->mrequest->save($id,$db);
					redirect(base_url("admin/request"));
				}else{
					if($id != NULL){
						$param          = array('r_id','r_email','r_contents');
						$data['result'] = $this->mrequest->get_info($id,$param);
						$this->load->view('layout',$data);
					}
				}
			}else{
				redirect(base_url("admin/request"));
			}
		}

		public function delete(){
			$id = $this->uri->segment(4);
			if($id !=NULL){	
				$this->mrequest->delete($id);
				redirect(base_url("admin/pages"));
			}
		}

		public function change_status(){
			if($this->input->post('action') != NULL){
				$data = array();
				$id   = (int)$this->input->post('id');
				if($this->input->post('status') == 'publish'){
					$data['status'] = 1;
					$this->mrequest->save($id,$data);
				}elseif($this->input->post('status') == 'unpublish'){
					$data['status'] = 2;
					$this->mrequest->save($id,$data);
				}
			}
		}

		public function count_unread(){
			$var           = array();
			$var['number'] = $this->mrequest->count_undred();
			exit(json_encode($var));
		}
	}